import sceneActions from 'actions/scene'
import { saveData } from 'utils'

export default {
  state: {}, // initial state
  reducers: {
    _logout (state) {
      return state
    },
    save (state, payload) {
      return saveData(state, payload)
    }
  },
  effects: dispatch => ({
    // 获取场景列表
    async getSceneList (params) {
      this.save({
        sceneData: null
      })
      const res = await sceneActions.getSceneList(params)
      if (res.success) {
        this.save({
          sceneData: {
            sceneList: res.data && res.data.sceneList,
            total: res.data && res.data.totalCount
          }
        })
      }
    },
    // 添加场景
    async addScene (params) {
      this.save({
        addRequest: false
      })
      const res = await sceneActions.addScene(params)
      if (res.success) {
        this.save({
          addRequest: true
        })
      }
    },
    // 上架
    async handlePutOn (params) {
      await sceneActions.handlePutOn(params)
    },
    // 推荐
    async handleRecommend (params) {
      await sceneActions.handleRecommend(params)
    },
    // 上新
    async handleNew (params) {
      await sceneActions.handleNew(params)
    },
    // 场景审核
    async handleCheck (params) {
      this.save({
        checkRequest: false
      })
      const res = await sceneActions.handleCheck(params)
      if (res.success) {
        this.save({
          checkRequest: true
        })
      }
    },
    // 场景审核状态设置
    async handleCheckEdit (params, state) {
      const arrCopy = state.scene.sceneData
      this.save({
        checkEditRequest: false
      })
      this.save({
        sceneData: null
      })
      setTimeout(() => {
        arrCopy.sceneList[params.index].auditStatus = params.auditStatus ? 'PASS' : 'REFUSE'
        this.save({
          checkEditRequest: true,
          sceneData: arrCopy
        })
      }, 100)
    },
    // 场景编辑
    async editScene (params) {
      this.save({
        editRequest: false
      })
      const res = await sceneActions.editScene(params)
      if (res.success) {
        this.save({
          editRequest: true
        })
      }
    },
    // 场景删除
    async deleteScene (params) {
      this.save({
        deleteRequest: false
      })
      const res = await sceneActions.deleteScene(params)
      if (res.success) {
        this.save({
          deleteRequest: true
        })
      }
    },
    // 场景还原
    async restoreReply (params) {
      this.save({
        restoreRequest: false
      })
      const res = await sceneActions.restoreReply(params)
      if (res.success) {
        this.save({
          restoreRequest: true
        })
      }
    },
    // 获取回收站列表
    async getRecycleList (params) {
      this.save({
        sceneRecycleData: null
      })
      const res = await sceneActions.getRecycleList(params)
      if (res.success) {
        this.save({
          sceneRecycleData: {
            sceneList: res.data && res.data.sceneList,
            total: res.data && res.data.totalCount
          }
        })
      }
    },
    // 获取日志记录列表
    async getLogList (params) {
      this.save({
        logData: null
      })
      const res = await sceneActions.getLogList(params)
      if (res.success) {
        this.save({
          logData: {
            logList: res.data && res.data.logList,
            total: res.data && res.data.totalCount
          }
        })
      }
    },
    // 获取场景详情
    async getSceneDetail (params) {
      this.save({
        details: null
      })
      const res = await sceneActions.getSceneDetail(params)
      if (res.success) {
        this.save({
          details: res.data
        })
      }
    },
    async fixScene (params) {
      this.save({
        fixRequest: null
      })
      const res = await sceneActions.fixScene(params)
      if (res.success) {
        this.save({
          fixRequest: res.data
        })
      }
    }
  })
}
